package com.cdtye.shuoh.service.product.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtye.shuoh.domain.product.entity.ProductProperties;
import com.cdtye.shuoh.domain.product.entity.ProductServices;
import com.cdtye.shuoh.domain.product.vo.ProductPropertiesVo;
import com.cdtye.shuoh.mapper.product.ProductPropertiesMapper;
import com.cdtye.shuoh.service.product.ProductPropertiesService;
import com.cdtye.shuoh.service.product.ProductServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductPropertiesServiceImpl extends ServiceImpl<ProductPropertiesMapper, ProductProperties> implements ProductPropertiesService {

    @Autowired
    private ProductServicesService productServicesService;

    public int insertProductProperties(ProductProperties productProperties){
        return this.baseMapper.insert(productProperties);
    }

    public IPage<ProductProperties> pageList(ProductPropertiesVo vo){
        return this.baseMapper.pageList(vo.page(),vo);
    }

    public List<ProductProperties> list(ProductPropertiesVo vo){
        return this.baseMapper.list(vo);
    }

    @Override
    public List<ProductProperties> getListByProductIdentification(String productIdentification) {
         List<ProductServices> services = productServicesService.findAllByProductIdentificationId(productIdentification);
         List<ProductProperties> result = new ArrayList<>();
        for (ProductServices service : services) {
            ProductPropertiesVo vo = new ProductPropertiesVo();
            vo.setServiceId(service.getId());
            result.addAll(this.baseMapper.list(vo));
        }
        return result;
    }

    @Override
    public int deleteByServiceId(String serviceId) {
        if(null!=serviceId){
            this.baseMapper.deleteByServiceId(serviceId);
        }
        return 0;
    }
}
